// 业务员进车管理专用JavaScript

document.addEventListener('DOMContentLoaded', function() {
    // 删除按钮确认
    const deleteButtons = document.querySelectorAll('.delete-btn');
    deleteButtons.forEach(button => {
        button.addEventListener('click', function(e) {
            if (!confirm('确定要删除此订单吗？此操作不可撤销。')) {
                e.preventDefault();
            }
        });
    });

    // 表单验证
    const orderForm = document.getElementById('orderForm');
    if (orderForm) {
        orderForm.addEventListener('submit', function(e) {
            const vehicleSelect = document.getElementById('vehicleSelect');
            const quantityInput = document.querySelector('input[name="quantity"]');

            let isValid = true;

            // 验证车辆选择
            if (!vehicleSelect.value) {
                isValid = false;
                alert('请选择车辆');
                vehicleSelect.focus();
            }

            // 验证数量
            if (!quantityInput.value || parseInt(quantityInput.value) <= 0) {
                isValid = false;
                alert('请输入有效的数量');
                quantityInput.focus();
            }

            if (!isValid) {
                e.preventDefault();
            }
        });
    }

    // 搜索功能
    const searchInput = document.getElementById('searchInput');
    if (searchInput) {
        searchInput.addEventListener('input', function() {
            const searchTerm = this.value.toLowerCase();
            const rows = document.querySelectorAll('.order-item');

            rows.forEach(row => {
                const rowText = row.textContent.toLowerCase();
                if (rowText.includes(searchTerm)) {
                    row.style.display = '';
                } else {
                    row.style.display = 'none';
                }
            });
        });
    }

    // 初始化数据表格
    const table = document.querySelector('table');
    if (table) {
        // 添加斑马纹效果
        const rows = table.querySelectorAll('tbody tr');
        rows.forEach((row, index) => {
            if (index % 2 === 0) {
                row.classList.add('table-row-even');
            } else {
                row.classList.add('table-row-odd');
            }
        });
    }
});